import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class MomService {
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数
  constructor(private http:HttpClient) { }
  private mom = '/MOM';
  CreateMOM(data:any){
    const url = this.mom+'/CreateMOM';
    return this.http.post(environment.apiUrl+ url,data);
  }// 创建物料

  GetMethodOfManufactures(searchTerm:string, page?: number, pageSize?: number,IsApproval?:any){
    let params = new HttpParams();
    if (searchTerm ) {
      params = params.set('SearchTerm', searchTerm);
    }
    if (page !== undefined && pageSize !== undefined) {
      params = params.set('PageNumber', page.toString());
      params = params.set('PageSize', pageSize.toString());
    }
    if(IsApproval){
      params = params.set('IsApproval', IsApproval);
    }
    return this.http.get(environment.apiUrl+ this.mom+'/GetMethodOfManufactures',{
      params: params
    })
  }
  GetMOMById(id:number){
    const url = this.mom+'/GetMOMById';
    return this.http.get(environment.apiUrl+ url,{params:{id}});
  }

  UpdateMOM(momId:number,data:any){
    const url = this.mom+'/UpdateMOM';
    return this.http.put(environment.apiUrl+ url,data,{params:{momId}});
  }
  DeleteMOMs(ids:any){
    const url = this.mom+'/DeleteMOMs';
    const options = {
      body: ids
    };
    return this.http.delete(environment.apiUrl+ url,options);
  }
  ApprovalMOM(momId:number){
    const url = this.mom+'/ApprovalMOM';
    let params = new HttpParams().set('momId', momId.toString());
    return this.http.put(environment.apiUrl+ url, null, {params: params});
  }

  UnApprovalMOM(momId:number){
    const url = this.mom+'/UnApprovalMOM';
    let params = new HttpParams().set('momId', momId.toString());
    return this.http.put(environment.apiUrl+ url, null, {params: params});
  }

  GetLogsByMOMId(momId:number){
    const url = this.mom+'/GetLogsByMOMId';
    return this.http.get(environment.apiUrl+ url,{params:{momId}});
  }
  UploadMOMAttachment(file:File){
    const url = this.mom+'/UploadMOMAttachment';
    const formData = new FormData();
    formData.append('file', file);
    return this.http.post(environment.apiUrl+ url,formData);
  }
}
